package vooga.entities;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;


public class Image extends Entity
{
    private java.awt.Image myImage = null;
   
    public Image (java.awt.geom.Point2D center,
	            java.awt.geom.Dimension2D size,
	            java.awt.Image graphic)
	{
	  super(center, size,
	        new java.awt.geom.Rectangle2D.Double(0, 0, 1, 1));
	  myImage = graphic;
	}
	
	public Image (java.awt.geom.Point2D center,
	            java.awt.geom.Dimension2D size,
	            String imageName)
	{
	  this(center, size, new javax.swing.ImageIcon(imageName).getImage());
	}    
    
    public void setImage(String image)
    {
    	myImage = new javax.swing.ImageIcon(image).getImage();
    }

/*
    protected java.awt.geom.AffineTransform getTransform ()
    {
	// world is mapped to be upside-down, so re-flip image so it appears upright
        java.awt.geom.AffineTransform result = new java.awt.geom.AffineTransform( 1, 0, 0, 
										 -1, 0, 0);
	result.concatenate(super.getTransform());
        return result;
    }
*/
    protected void render (java.awt.Graphics2D g)
    {
    	//backup current transforom
    	java.awt.geom.AffineTransform oldG = g.getTransform();
    	g.transform(getTransform());
    	
    	//backup current composite (we use it for transparency)
    	Composite oldC = g.getComposite();
    	g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, getColor().getAlpha() / 255f));
        
    	g.drawImage(myImage, 0, 0, 1, 1, null);
        
    	//restore
        g.setComposite(oldC);
    	g.setTransform(oldG);
    }
}
